package com.renhui.android.handwriting.eventbus;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.renhui.android.handwriting.R;
import com.renhui.android.handwriting.hermes.EvenMulitProcessActivity;

public class EventBusExampleActivity extends AppCompatActivity {

    TextView textView;
    Button button;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);
        setContentView(R.layout.activity_event_bus_example);
        textView = findViewById(R.id.text);
        button = findViewById(R.id.send_message);
        button.setOnClickListener(v -> {
            startActivity(new Intent(EventBusExampleActivity.this, EvenMulitProcessActivity.class));
        });
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventBusCome(String message) {
        Log.e("EventBus",  Thread.currentThread().getName());
        Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
        textView.setText(message);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unRegister(this);
    }
}